/*******************************************************************************
 * Copyright (c) 2010 NineConsult A/S (http://nine.dk) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package dk.nineconsult.jmoller.mapr.scoping;

import org.eclipse.emf.ecore.EReference;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;

import com.google.inject.Inject;

import dk.nineconsult.jmoller.mapr.mapr.FeatureMap;
import dk.nineconsult.jmoller.mapr.mapr.Mapping;

/**
 * This class contains custom scoping description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping
 * on how and when to use it 
 *
 */
public class MaprScopeProvider extends AbstractDeclarativeScopeProvider {


	@Inject
	private IBeanPatternFeatureLookup featureLookup;

	public IScope scope_FeatureMap_leftField(FeatureMap featureMap, EReference reference) {
		Mapping mapping = EcoreUtil2.getContainerOfType(featureMap, Mapping.class);
		if (mapping == null || mapping.getLeftClass() == null)
			return IScope.NULLSCOPE;
		
		return new MapBasedScope(featureLookup.getBeanPatternFeatures(mapping.getLeftClass()));
	}

	public IScope scope_FeatureMap_rightField(FeatureMap featureMap, EReference reference) {
		Mapping mapping = EcoreUtil2.getContainerOfType(featureMap, Mapping.class);
		if (mapping == null || mapping.getRightClass() == null)
			return IScope.NULLSCOPE;
		
		return new MapBasedScope(featureLookup.getBeanPatternFeatures(mapping.getRightClass()));
	}
		
	public void setFeatureLookup(IBeanPatternFeatureLookup featureLookup) {
		this.featureLookup = featureLookup;
	}

	public IBeanPatternFeatureLookup getFeatureLookup() {
		return featureLookup;
	}

}
